package com.yuetong.quotation.service.impl;

import java.util.List;

import com.yuetong.common.utils.DateUtils;
import com.yuetong.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.yuetong.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yuetong.quotation.domain.YtQuotationDetail;
import com.yuetong.quotation.mapper.YtQuotationInfoMapper;
import com.yuetong.quotation.domain.YtQuotationInfo;
import com.yuetong.quotation.service.IYtQuotationInfoService;

/**
 * 报价Service业务层处理
 *
 * @author  卓晓君
 * @date 2025-02-11
 */
@Service
public class YtQuotationInfoServiceImpl implements IYtQuotationInfoService
{
    @Autowired
    private YtQuotationInfoMapper ytQuotationInfoMapper;

    /**
     * 查询报价
     *
     * @param quotationId 报价主键
     * @return 报价
     */
    @Override
    public YtQuotationInfo selectYtQuotationInfoByQuotationId(Long quotationId)
    {
        return ytQuotationInfoMapper.selectYtQuotationInfoByQuotationId(quotationId);
    }

    /**
     * 查询报价列表
     *
     * @param ytQuotationInfo 报价
     * @return 报价
     */
    @Override
    public List<YtQuotationInfo> selectYtQuotationInfoList(YtQuotationInfo ytQuotationInfo)
    {
        return ytQuotationInfoMapper.selectYtQuotationInfoList(ytQuotationInfo);
    }

    /**
     * 新增报价
     *
     * @param ytQuotationInfo 报价
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYtQuotationInfo(YtQuotationInfo ytQuotationInfo)
    {
        ytQuotationInfo.setCreateTime(DateUtils.getNowDate());
        ytQuotationInfo.setCreatedBy(SecurityUtils.getUsername());
        int rows = ytQuotationInfoMapper.insertYtQuotationInfo(ytQuotationInfo);
        insertYtQuotationDetail(ytQuotationInfo);
        return rows;
    }

    /**
     * 修改报价
     *
     * @param ytQuotationInfo 报价
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYtQuotationInfo(YtQuotationInfo ytQuotationInfo)
    {
        ytQuotationInfo.setUpdateTime(DateUtils.getNowDate());
        ytQuotationInfo.setUpdatedBy(SecurityUtils.getUsername());
        ytQuotationInfoMapper.deleteYtQuotationDetailByQuotationId(ytQuotationInfo.getQuotationId());
        insertYtQuotationDetail(ytQuotationInfo);
        return ytQuotationInfoMapper.updateYtQuotationInfo(ytQuotationInfo);
    }

    /**
     * 批量删除报价
     *
     * @param quotationIds 需要删除的报价主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYtQuotationInfoByQuotationIds(Long[] quotationIds)
    {
        ytQuotationInfoMapper.deleteYtQuotationDetailByQuotationIds(quotationIds);
        return ytQuotationInfoMapper.deleteYtQuotationInfoByQuotationIds(quotationIds);
    }

    /**
     * 删除报价信息
     *
     * @param quotationId 报价主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYtQuotationInfoByQuotationId(Long quotationId)
    {
        ytQuotationInfoMapper.deleteYtQuotationDetailByQuotationId(quotationId);
        return ytQuotationInfoMapper.deleteYtQuotationInfoByQuotationId(quotationId);
    }

    /**
     * 新增报价详情明细信息
     *
     * @param ytQuotationInfo 报价对象
     */
    public void insertYtQuotationDetail(YtQuotationInfo ytQuotationInfo)
    {
        List<YtQuotationDetail> ytQuotationDetailList = ytQuotationInfo.getYtQuotationDetailList();
        Long quotationId = ytQuotationInfo.getQuotationId();
        if (StringUtils.isNotNull(ytQuotationDetailList))
        {
            List<YtQuotationDetail> list = new ArrayList<YtQuotationDetail>();
            for (YtQuotationDetail ytQuotationDetail : ytQuotationDetailList)
            {
                ytQuotationDetail.setQuotationId(quotationId);
                list.add(ytQuotationDetail);
            }
            if (list.size() > 0)
            {
                ytQuotationInfoMapper.batchYtQuotationDetail(list);
            }
        }
    }
}
